package com.ydzt.fanka.server.init;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;

import com.ydzt.fanka.server.domain.ParamDefine;
import com.ydzt.fanka.server.utils.ConfigFileUtil;
import com.ydzt.fanka.server.utils.DBHelper;
import com.ydzt.fanka.server.utils.FileUtil;
import com.ydzt.fanka.server.utils.XmlUtil;

public class InitData {
	static Logger logger = Logger.getLogger(InitData.class.getName());
	
	public static long DAY = 24L * 60L * 60L * 1000L; 
	private static String parmPath;
	private static HashMap<String,String> supplierMap;
	
	public final static String auto_shift = ConfigFileUtil.GetValueByKey("AUTO_SHIFT");
	
	public final static String jdbc_connect_error = "jdbc_connect_error";
	
	public final static String write_into_buffer_error = "write_into_buffer_error";
	
	public final static String tmpdata = ConfigFileUtil.GetValueByKey("TMP_DATA");
	
	public final static String endflag = ConfigFileUtil.GetValueByKey("END_FLAG");
	
	public final static String nodeType = ConfigFileUtil.GetValueByKey("NODE_TYPE");
	
	public final static String paramDefinePath = ConfigFileUtil.GetValueByKey("PARAMDEFINEPATH");
	
	public final static String bufferPath = ConfigFileUtil.GetValueByKey("BUFFERPATH");
	
	public final static String backupPath = ConfigFileUtil.GetValueByKey("BACKUPPATH");
	
	public final static String paramDefineFile = ConfigFileUtil.GetValueByKey("PARAMDEFINEFILE");
	
	public final static String onlyReadPath = ConfigFileUtil.GetValueByKey("ONLYREAD");
	
	public final static String transmitPath = ConfigFileUtil.GetValueByKey("TRANSMITPATH");
	
	public final static String errorPath = ConfigFileUtil.GetValueByKey("ERRORPATH");
	
	public final static String wait_transmitPath = ConfigFileUtil.GetValueByKey("WAIT_TRANSMITPATH");
	
	public final static String backup_transmitPath = ConfigFileUtil.GetValueByKey("BACKUP_TRANSMITPATH");
	
	public final static String pkgLength = ConfigFileUtil.GetValueByKey("PKG_LEN");
	
	public final static String transmit_max_record = ConfigFileUtil.GetValueByKey("TRANSMIT_MAX_RECORD");
	
	public final static String transmit_timeout = ConfigFileUtil.GetValueByKey("TRANSMIT_TIME_OUT");
	
	public final static String broadcastlist = ConfigFileUtil.GetValueByKey("BROADCAST_LIST");
	
	public final static String broadcastdir = ConfigFileUtil.GetValueByKey("BROADCAST_DIR");
	
    public final static String broadcast_max_record = ConfigFileUtil.GetValueByKey("BROADCAST_MAX_RECORD");
	
	public final static String broadcast_timeout = ConfigFileUtil.GetValueByKey("BROADCAST_TIME_OUT");
	
	public final static String backup_broadcast = ConfigFileUtil.GetValueByKey("BACKUP_BROADCAST");
	
	public final static String center_ip = ConfigFileUtil.GetValueByKey("CENTER_IP");
	
	public final static String center_port = ConfigFileUtil.GetValueByKey("CENTER_PORT");
	
	public final static String HOUR_OF_DAY = ConfigFileUtil.GetValueByKey("HOUR_OF_DAY");
	
	public final static String MINUTE = ConfigFileUtil.GetValueByKey("MINUTE");
	
	public final static String SECOND = ConfigFileUtil.GetValueByKey("SECOND");
	
	public final static String PERIOD_DR_ENDDAY = ConfigFileUtil.GetValueByKey("PERIOD_DR_ENDDAY");
	
	public final static String DR_TMP_DATA = ConfigFileUtil.GetValueByKey("DR_TMP_DATA");
	
	public final static String OWNER_IP = ConfigFileUtil.GetValueByKey("OWNER_IP");
	
	public final static String OWNER_PORT = ConfigFileUtil.GetValueByKey("OWNER_PORT");
	
	public final static String DR_PORT = ConfigFileUtil.GetValueByKey("DR_PORT");
	
	public final static String DR_BUFFER_DATA = ConfigFileUtil.GetValueByKey("DR_BUFFER_DATA");
	
	public final static String DR_ONLYREAD_DATA = ConfigFileUtil.GetValueByKey("DR_ONLYREAD_DATA");
	
	//中心使用此广播列表  此广播列表记录的是食堂
	public final static String dr_map = ConfigFileUtil.GetValueByKey("DR_MAP");
	
	//食堂使用此广播列表 此广播列表记录的是POS
	public final static String pos_list = ConfigFileUtil.GetValueByKey("POS_LIST");
	
	public final static String param_table_mapping = ConfigFileUtil.GetValueByKey("PARAM_TABLE_MAP");
	
	public final static String communication_pos = ConfigFileUtil.GetValueByKey("COMMUNICATION_DLL");
	
	private InitData(){
		parmPath = InitData.paramDefineFile;
		supplierMap  = DBHelper.getSupplierAndIPMap();
	}
	private static final InitData initData = new InitData();
	
	
	
	
	public HashMap<String, String> getSupplierMap() {
		return supplierMap;
	}

	public static InitData getInstance(){
		return initData;
	}
	
	public static HashMap<String , ParamDefine> initParamDefine(){
		String path = null;
		HashMap<String , ParamDefine> map = new HashMap<String , ParamDefine>();  
		List<String> fileList = FileUtil.getFileList(InitData.paramDefineFile);
		if(fileList != null){
			if(fileList.size() != 0){
				for(int i = 0; i < fileList.size(); i ++){
					ParamDefine paramDefine = new ParamDefine();
					path = parmPath + "/" + fileList.get(i).toString();
					Document doc = XmlUtil.ReadXml(path);
					List<Element> nodes = XmlUtil.GetNodes(doc);
					String tablename = XmlUtil.GetRootName(doc);
					int length = XmlUtil.getRootNodeAttributeLength(doc);
					//int count = XmlUtil.getRootNodeAttributeCount(doc);
					paramDefine.setStructure(nodes);
					paramDefine.setTablename(tablename);
					paramDefine.setOneRecordLength(length);
					map.put(fileList.get(i).toString().substring(0, 4), paramDefine);
				}
				logger.info("初始化参数定义成功");
			}
			else{
				logger.info("参数定义文件夹中无参数定义文件");
			}
		}
		else{
			logger.info("参数定义文件夹为空");
		}
		
		return map;
	}
}